package cn.just.controller;

import cn.just.entity.Article;
import cn.just.entity.Link;
import cn.just.entity.Tag;
import cn.just.entity.User;
import cn.just.service.ArticleService;
import cn.just.service.LinkService;
import cn.just.service.TagService;
import cn.just.utils.THUtils;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author wujiaming
 */
@WebServlet(name = "HomeServlet",value="/HomeServlet")
public class HomeServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        if(user == null){
            response.sendRedirect("ShowLoginServlet");
            return;
        }

        //先显示index.html 假数据页面
        Context context = new Context();
        //将当前登录的用户名添加到容器中
        context.setVariable("userName",user.getUserName());

        //查询8篇最新置顶文章
        ArticleService articleService = new ArticleService();
        List<Article> homeList = articleService.getHomeList();

        //把第一篇文章传递到页面中显示
        context.setVariable("first",homeList.get(0));


        //把第2-4篇文章传递页面中显示 使用subList(fromIndex,toIndex)下标左闭右开
        List<Article> second = homeList.subList(1, 4);
        context.setVariable("second",second);

        //把第5-8篇文章传递页面中显示
        List<Article> thirdly = homeList.subList(4, 8);
        context.setVariable("thirdly",thirdly);

        //查询最新文章列表
        List<Article> newList = articleService.getNewList();
//        System.out.println(newList.size());
//        for(int i=0;i<newList.size();i++){
//            System.out.println(newList.get(i).getTitle());
//        }
        context.setVariable("newList",newList);

        //查询评论最多的文章
        List<Article> maxCommentArticle = articleService.getMaxCommentArticle();
        context.setVariable("maxCommentArticle",maxCommentArticle);

        //查询评论最多的文章
        List<Article> maxViewArticle = articleService.getMaxViewArticle();
        context.setVariable("maxViewArticle",maxViewArticle);

        //查询标签没引用对多的
        TagService tagService = new TagService();
        List<Tag> tags = tagService.findTagByreferenceCount();
        context.setVariable("tags",tags);

        //友情链接,查询所有的链接名称和地址
        LinkService linkService = new LinkService();
        List<Link> links = linkService.findAll();
//        System.out.println(links.size());
//        for(int i=0;i<links.size();i++){
//            System.out.println(links.get(i).getAddress() + links.get(i).getTitle());
//        }
        context.setVariable("links",links);

        THUtils.write("blog/index",context,response);
    }
}
